package com.azusa.ktc_base.controller;

import com.azusa.ktc_base.entity.City;
import com.azusa.ktc_base.service.CityService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("city")
public class CityController {
    
    @Autowired
    private CityService service;

    /**
     * 返回标签列表
     * @return
     */
    @GetMapping
    public Result findAll(){
        return new Result(StatusCode.SUCCESS,service.findAll());
    }

    /**
     * 添加标签记录
     * @param city
     * @return
     */
    @PostMapping
    public Result insertCity(@RequestBody City city){
        return new Result(StatusCode.SUCCESS,service.save(city));
    }

    /**
     * 根据id查询标签
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id){
        return new Result(StatusCode.SUCCESS,service.findById(id));
    }

    /**
     * 根据id修改标签记录
     * @param city
     * @return
     */
    @PutMapping("/{id}")
    public Result updateById(@RequestBody City city){
        return new Result(StatusCode.SUCCESS,service.save(city));
    }

    /**
     * 根据id删除标签记录
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") String id){
        service.deleteById(id);
        return new Result(StatusCode.SUCCESS);
    }

    /**
     * 查询分页
     * @param page
     * @param size
     * @return
     */
    @PostMapping("search/{page}/{size}")
    public Result findPage(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        Page<City> CityPage = service.findPage(page, size);
        return new Result(StatusCode.SUCCESS,new PageResult(CityPage.getTotalElements(),CityPage.getContent()));
    }


    /**
     * 根据条件查询标签记录
     * @param city
     * @return
     */
    @PostMapping("search")
    public Result findSearch(@RequestBody City city){
        return new Result(StatusCode.SUCCESS,service.findSearch(city));
    }
    
}
